package com.neulogistics.core.substation.api.application.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.core.substation.api.application.viewmodel.TaskOrderViewModel;
import com.neulogistics.core.substation.domain.model.valueobject.Address;
import com.neulogistics.core.substation.domain.model.valueobject.CourierInfo;
import com.neulogistics.core.substation.domain.model.valueobject.CustomInfo;
import com.neulogistics.core.substation.domain.model.valueobject.OrderInfo;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.TaskOrderPO;
import org.springframework.beans.BeanUtils;

public class TaskOrderPOVMConverter {
    public static TaskOrderViewModel POToVM(TaskOrderPO taskOrderPO){
        if (taskOrderPO == null)
            return null;

        TaskOrderViewModel taskOrderViewModel = new TaskOrderViewModel();
        BeanUtils.copyProperties(taskOrderPO, taskOrderViewModel);
        // long转string
        taskOrderViewModel.setTaskId(taskOrderPO.getTaskId().toString());
        if(taskOrderPO.getInvoiceId() != null){
            taskOrderViewModel.setInvoiceId(taskOrderPO.getInvoiceId().toString());
        }
        // json对象转换
        taskOrderViewModel.setOrderInfo(JSONUtil.toBean(taskOrderPO.getOrderInfo(), OrderInfo.class));
        taskOrderViewModel.setCourierInfo(JSONUtil.toBean(taskOrderPO.getCourierInfo(), CourierInfo.class));
        taskOrderViewModel.setCustomInfo(JSONUtil.toBean(taskOrderPO.getCustomInfo(), CustomInfo.class));

        return taskOrderViewModel;
    }
}
